package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"time"
)

type ShangjuansRBody struct {
	Data       []models.Usershangjuan `json:"data"`
	PageNo     int                    `json:"pageNo"`
	PageSize   int                    `json:"pageSize"`
	TotalCount int64                  `json:"totalCount"`
	TotalPage  int64                  `json:"totalPage"`
}

type ShangjuansBody struct {
	Page        int   `json:"page"`
	Pagesize    int   `json:"pagesize"`
	Classify    int64 `json:"classify" gorm:"classify"` // 1待分享 2已分享 3已领取
	Uid         int64 `json:"uid" gorm:"uid"`
	Shangjuanid int64 `json:"shangjuanid" gorm:"shangjuanid"`
	ID          int64 `json:"id" gorm:"id"`
}

func Shangjuans(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Usershangjuan
	res := data_source.Db.Where("uid = ?", uid)

	if classify == 1 {
		res = res.Where("status = 1")
	}
	if classify == 2 {
		res = res.Where("status = 2 or status = 3")
	}
	res.Model(&models.Usershangjuan{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShangjuansRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type ShangjuanylqRBody struct {
	Data       []models.Shangjuandianliang `json:"data"`
	PageNo     int                         `json:"pageNo"`
	PageSize   int                         `json:"pageSize"`
	TotalCount int64                       `json:"totalCount"`
	TotalPage  int64                       `json:"totalPage"`
}

func Shangjuanylq(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Shangjuandianliang
	res := data_source.Db.Where("uid = ?", uid)
	res.Model(&models.Shangjuandianliang{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShangjuanylqRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type ShangjuanfxRBody struct {
	Data       []models.Shangjuandianliangsql `json:"data"`
	Shangjuan  models.Shangjuan               `json:"shangjuan"`
	PageNo     int                            `json:"pageNo"`
	PageSize   int                            `json:"pageSize"`
	TotalCount int64                          `json:"totalCount"`
	TotalPage  int64                          `json:"totalPage"`
}

func Shangjuanfxlist(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	shangjuanid := request.Shangjuanid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64

	var usershangjuan models.Usershangjuan
	data_source.Db.Where("id = ?", shangjuanid).First(&usershangjuan)

	var users []models.Shangjuandianliangsql
	var shangjuan models.Shangjuan
	data_source.Db.Where("id = ?", usershangjuan.Shangjuanid).First(&shangjuan)

	res := data_source.Db.Model(&models.Shangjuandianliang{}).Select("shangjuandianliang.*, user.nickname").Joins("left join user on user.id = shangjuandianliang.uid").Where("shangjuandianliang.usershangjuanid = ?", shangjuanid)

	res.Model(&models.Shangjuandianliang{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShangjuanfxRBody{
		Data:       users,
		Shangjuan:  shangjuan,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Shangjuaninfo(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	var users models.Usershangjuan
	data_source.Db.Where("id = ?", id).First(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return
}

func Shangjuandl(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var sj models.Usershangjuan
	data_source.Db.Where("id = ?", id).First(&sj)
	if sj.Uid == uid {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "不能给自己点亮",
		})
		return
	}
	var dl []models.Shangjuandianliang
	data_source.Db.Where("usershangjuanid = ?", id).Find(&dl)
	if int64(len(dl)) == sj.Num {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "对不起，已经全部点亮了",
		})
		return
	}
	for _, v := range dl {
		if v.Uid == uid {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请勿重复点亮",
			})
			return
		}
	}
	dladd := models.Shangjuandianliang{
		Uid:             uid,
		Addtime:         time.Now().Unix(),
		Usershangjuanid: id,
		Daibi:           sj.Daibi,
	}
	data_source.Db.Create(&dladd)
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	userdaibi := models.Userdaibi{
		Uid:     uid,
		Status:  1,
		Addtime: time.Now().Unix(),
		Daibi:   sj.Userdaibi,
		Title:   "点亮赏券获得矿石",
		State:   3,
		Sydaibi: user.Daibi + sj.Userdaibi,
	}
	data_source.Db.Create(&userdaibi)

	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("daibi", gorm.Expr("daibi + ?", sj.Userdaibi))
	data_source.Db.Model(&models.Usershangjuan{}).Where("id = ?", id).Update("seccessnum", gorm.Expr("seccessnum + ?", 1))

	var user1 models.User
	data_source.Db.Where("id = ?", sj.Uid).First(&user1)
	userdaibi1 := models.Userdaibi{
		Uid:     sj.Uid,
		Status:  1,
		Addtime: time.Now().Unix(),
		Daibi:   sj.Daibi / float64(sj.Num),
		Title:   "赏券被点亮获得矿石",
		State:   4,
		Sydaibi: user1.Daibi + sj.Daibi/float64(sj.Num),
	}
	data_source.Db.Create(&userdaibi1)

	data_source.Db.Model(&models.User{}).Where("id = ?", sj.Uid).Update("daibi", gorm.Expr("daibi + ?", sj.Daibi/float64(sj.Num)))

	if len(dl) == 0 {
		data_source.Db.Model(&models.Usershangjuan{}).Where("id = ?", id).Update("status", 2)
	}

	if sj.Num-int64(len(dl)) == 1 {
		data_source.Db.Model(&models.Usershangjuan{}).Where("id = ?", id).Update("status", 3)
		var user_info models.User
		data_source.Db.Where("id = ?", sj.Uid).First(&user_info)

	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Shangjuanfx(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var usershangjuan models.Usershangjuan
	data_source.Db.Where("id = ?", id).First(&usershangjuan)
	if usershangjuan.Status == 1 {
		data_source.Db.Model(&models.Usershangjuan{}).Where("id = ?", id).Update("status", 2)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
